package com.shushan.mixedexam.strangewords;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.shushan.base.BaseFragment;
import com.shushan.mixedexam.bean.ExerciseBean;
import com.shushan.pages.newtype.bean.NewWordBean;
import com.shushan.pages.newtype.bean.StrangeWordsStudyItemBean;
import com.shushan.shushanbao.R;
import com.shushan.ui.OffsetRecyclerDivider;
import com.shushan.util.DensityUtil;
import com.shushan.util.DownloadUtil;
import com.shushan.util.ImageUtils;

import java.io.File;

/**
 * @ClassName: StrangeWordsStudyFragment
 * @Desciption: //生字学一学fragment
 * @author: zhangshihao
 * @date: 2018-06-21
 */
public class StrangeWordsStudyFragment extends BaseFragment implements StrangeWordsStudyAdapter.OnTrumpetClickListener{

    private static final String TAG = StrangeWordsStudyFragment.class.getSimpleName();

    private View contentView;
    private RecyclerView recyclerView;
    private ImageView ivChineseSpell;
    private TextView tvStrokesCount;
    private TextView tvStructureDesc;
    private TextView tvRadicalsDesc;
    private TextView tvCombineWords;

    private NewWordBean itemBean;
    private int examIndex;
    private int outLineDetailId;
    private OnWordTrumpetClickListener onWordTrumpetClickListener;

    public static StrangeWordsStudyFragment newInstance(int examIndex, NewWordBean itemBean,int outLineDetailId) {
        StrangeWordsStudyFragment studyFragment = new StrangeWordsStudyFragment();
        studyFragment.setExamIndex(examIndex);
        studyFragment.setItemBean(itemBean);
        studyFragment.setOutLineDetailId(outLineDetailId);

        return studyFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_strange_words_study, null, false);
            initView();
            initData();
        }
        return contentView;
    }

    private void initView(){
        recyclerView = contentView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new OffsetRecyclerDivider(DensityUtil.dip2px(getContext(),10f)));
        ivChineseSpell = contentView.findViewById(R.id.iv_chinese_spell);
        tvStrokesCount = contentView.findViewById(R.id.tv_strokes_count);
        tvStructureDesc = contentView.findViewById(R.id.tv_structure_desc);
        tvRadicalsDesc = contentView.findViewById(R.id.tv_radicals_desc);
        tvCombineWords = contentView.findViewById(R.id.tv_combine_words);
    }

    private void initData(){
        if(itemBean == null){
            Log.e(TAG,"itemBean == null");
            return;
        }
        StrangeWordsStudyAdapter studyAdapter = new StrangeWordsStudyAdapter(itemBean.getPhraseItemBeans());
        studyAdapter.setOnTrumpetClickListener(this);
        File gifFile = new File(DownloadUtil.getStrangeWordsStudyAudioPath(itemBean,outLineDetailId,0,DownloadUtil.TYPE_GIF));
        if(gifFile.exists()){
            ImageUtils.loadImgFile(getActivity(),gifFile,ivChineseSpell);
        }else{
            ImageUtils.loadGifImageView(getActivity(),itemBean.gif_url,ivChineseSpell);
        }
        recyclerView.setAdapter(studyAdapter);
        tvStrokesCount.setText(itemBean.bihua+"");
        tvStructureDesc.setText(itemBean.jiegou);
        tvRadicalsDesc.setText(itemBean.bushou);
        tvCombineWords.setText(itemBean.zuci);
    }

    public void setItemBean(NewWordBean itemBean) {
        this.itemBean = itemBean;
    }

    public void setExamIndex(int examIndex) {
        this.examIndex = examIndex;
    }

    @Override
    public void onTrumpetClick(int position,AnimationDrawable playAnim) {
        if(onWordTrumpetClickListener != null){
            onWordTrumpetClickListener.onListTrumpetClick(itemBean.id,position+1,playAnim);
        }
    }

    public interface OnWordTrumpetClickListener{
        void onListTrumpetClick(int wordId,int index,AnimationDrawable playAnim);
    }

    public void setOnWordTrumpetClickListener(OnWordTrumpetClickListener onWordTrumpetClickListener) {
        this.onWordTrumpetClickListener = onWordTrumpetClickListener;
    }

    public int getOutLineDetailId() {
        return outLineDetailId;
    }

    public void setOutLineDetailId(int outLineDetailId) {
        this.outLineDetailId = outLineDetailId;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }
}
